package ch06;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

import java.util.Arrays;

public class PersonLog {


	public void before(JoinPoint jp) {
		System.out.println("==========前置通知开始============");
		System.out.println("调用 " + jp.getTarget() + " 的 " + jp.getSignature().getName()
				+ " 方法。方法入参：" + Arrays.toString(jp.getArgs()));
		System.out.println("==========打开数据库连接=============");

	}
	
	public void afterReturning(JoinPoint jp, Object result) {
		System.out.println("==============后置通知开始===============");
		System.out.println(jp.getTarget() + "---" + jp.getSignature().getName() + "--" + jp.getArgs() + "--" + result);
	}

	public void afterThrowing(JoinPoint jp, RuntimeException e) {
		System.out.println(jp.getSignature().getName() + " 方法发生异常：" + e);
	}

	public void afterLogger(JoinPoint jp) {
		System.out.println(jp.getSignature().getName() + " 方法结束执行。");
	}

	public Object aroundLogger(ProceedingJoinPoint jp) throws Throwable {
		System.out.println("调用 " + jp.getTarget() + " 的 " + jp.getSignature().getName()
				+ " 方法。方法入参：" + Arrays.toString(jp.getArgs()));
		try {
			Object result = jp.proceed();
			System.out.println("调用 " + jp.getTarget() + " 的 "
					+ jp.getSignature().getName() + " 方法。方法返回值：" + result);
			return result;
		} catch (Throwable e) {
			System.out.println(jp.getSignature().getName() + " 方法发生异常：" + e);
			throw e;
		} finally {
			System.out.println(jp.getSignature().getName() + " 方法结束执行。");
		}

	}

}